package com.bibrApp.difu.actvity.splash_mvp;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.bibrApp.difu.Config;
import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.actvity.main_mvp.MainActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.utils.BiBrShare;
import com.bibrApp.difu.utils.IntentUtil;
import com.bibrApp.difu.utils.ShareUtils;
import com.gyf.barlibrary.ImmersionBar;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 初始化页面
 */
public class SplashActivity extends BaseActivity<SplashPresenter> implements ISplashView, EasyPermissions.PermissionCallbacks {

    private Context mContext;
    private ImmersionBar mImmersionBar;
    private ShareUtils<LoginResponseBody> biBrShare;
    public static final int PERMISSION_REQ = 0x111;
    public static final String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

    @Override
    protected void initPresenter() {
        presenter = new SplashPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void success(String msg) {

    }

    @Override
    public void initView() {
        mContext = this;
        biBrShare = BiBrShare.getShare(mContext, Config.FILE_ACCOUNT);

        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.barColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();


        initUserData();
    }


    private void initUserData() {
        LoginResponseBody data = biBrShare.<LoginResponseBody>getData(Config.FILE_ACCOUNT_KEY);
        if (data != null) {
            DfApp app = (DfApp) getApplication();
            if (!TextUtils.isEmpty(data.getCenterUserMain().getMemberId())) {
                app.setuId(data.getCenterUserMain().getMemberId());
            }
            if (!TextUtils.isEmpty(data.getCenterUserMain().getId())) {
                app.setUserId(data.getCenterUserMain().getId());
            }
            if (!TextUtils.isEmpty(data.getCenterUserMain().getNickImage())) {
                app.setNickImage(data.getCenterUserMain().getNickImage());
            }
            if (!TextUtils.isEmpty(data.getCenterUserMain().getId())) {
                app.setUserId(data.getCenterUserMain().getId());
            }
            if (!TextUtils.isEmpty(data.getTokenResultBO().getAccess_token())) {
                app.setToken(data.getTokenResultBO().getAccess_token());
            }
            if (!TextUtils.isEmpty(data.getCenterUserMain().getMemberId())) {
                app.setMemberId(data.getCenterUserMain().getMemberId());
            }
            app.setmData(data);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                IntentUtil.toActivity(MainActivity.class, mContext);
                finish();
            }
        }, 1500);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
    }


    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PERMISSION_REQ) {
            initUserData();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this)
                    .setTitle("设置权限")
                    .setRationale("需要开启存储权限，才能更好体验App")
                    .setPositiveButton("设置")
                    .build()
                    .show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQ && grantResults.length == 2) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                initUserData();
            } else if (grantResults[0] == PackageManager.PERMISSION_DENIED || grantResults[1] == PackageManager.PERMISSION_DENIED) {
                new AppSettingsDialog.Builder(this)
                        .setTitle("设置权限")
                        .setRationale("需要开启存储权限，才能更好体验App")
                        .setPositiveButton("设置")
                        .setNegativeButton(" ")
                        .build()
                        .show();
                /*ToastUtils.ShowToast(mContext, "请先开启存储权限");
                finish();*/
            }
        }
        // Forward results to EasyPermissions
//        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            if (EasyPermissions.hasPermissions(this, permissions)) {
                initUserData();
            } else {
                EasyPermissions.requestPermissions(this, "需要开启存储权限，才能更好体验App", PERMISSION_REQ, permissions);
            }
        }
    }
}
